﻿var imageUploadUrl = "";
$(function () {
    $.ajax({
        url: '../rds_user/getUserById',
        type: 'post',
        data: {'userId': gUserLoginResult.userId},
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData.length != 0) {
                        $("#userId").val(msg.jsonData.userId);
                        $("#loginName").val(msg.jsonData.loginName);
                        $("#userName").val(msg.jsonData.userName);
                        if (msg.jsonData.userSex == "男") {
                            $("input[name='mySex'][value='男']").attr("checked", true);
                        }
                        else {
                            $("input[name='mySex'][value='女']").attr("checked", true);
                        }
                        $("#userMobile").val(msg.jsonData.userMobile);
                        $("#userEmail").val(msg.jsonData.userEmail);
                        $("#userRemarks").val(msg.jsonData.userRemarks);
                        $("#userIcon").val(msg.jsonData.userIcon);
                        if (msg.jsonData.userIcon == "" || msg.jsonData.userIcon == null) {
                            if (msg.jsonData.userSex == "男") {
                                $("#myPhoto").attr('src', src = "../images/default_male.png");
                            }
                            else {
                                $("#myPhoto").attr('src', src = "../images/default_female.png");
                            }
                        } else {
                            $("#myPhoto").attr('src', src = msg.jsonData.userIcon);
                        }
                    } else {
                        $.alert("获取用户信息失败，请刷新页面", 2);
                    }
                } else {
                    $.alert("获取用户信息失败，请刷新页面", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
        },
        error: function (msg) {
            $.alert('系统错误，请联系管理员', 2);
        }
    });
});

// 用户信息修改
$('#form-myinfo').bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        userName: {
            validators: {
                notEmpty: {message: '姓名不能为空！'}
            }
        },
        userMobile: {
            validators: {
                notEmpty: {message: '联系电话不能为空'}
            }
        },
        userEmail: {
            validators: {
                notEmpty: {message: '邮箱不能为空'},
                emailAddress: {message: '邮箱地址格式有误'}
            }
        }
    }
}).on('success.form.bv', function (e) {
    e.preventDefault();
    $("#userSex").val($("input:radio[name='mySex']:checked").val());
    var $form = $(e.target);
    $.ajax({
        url: "../rds_user/updateUserById",
        type: 'post',
        data: $form.serialize(),
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg.jsonData > 0) {
                        $.alert("数据操作成功", 1);
                        $("#myName-middle").html($("#userName").val());
                        $("#myName-small").html($("#userName").val());
                        if (imageUploadUrl != "") {
                            $("#myPhoto-large").attr('src', src = imageUploadUrl);
                            $("#myPhoto-middle").attr('src', src = imageUploadUrl);
                            $("#myPhoto-small").attr('src', src = imageUploadUrl);
                        }
                    } else {
                        $.alert("数据操作失败", 2);
                    }
                } else {
                    $.alert("数据操作失败", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
            $('#form-myinfo').bootstrapValidator('disableSubmitButtons', false);
        },
        error: function (msg) {
            $.alert('系统错误，请联系管理员', 2);
            $('#form-myinfo').bootstrapValidator('disableSubmitButtons', false);
        }
    });
});
// 打开上传图片的对话框
$.openImageUpload = function () {
    $("#modal-upload-image").modal({
        backdrop: 'static',
        keyboard: true,
        show: true
    });
};
// 阿里云判断上传图片类型，在需要的js文件添加，文件名务必保持一致
function judgeFileType(suffix) {
    if (!(suffix == ".jpg" || suffix == ".JPG" || suffix == ".png"
        || suffix == ".PNG" || suffix == ".jpeg" || suffix == ".JPEG"
        || suffix == ".gif" || suffix == ".GIF" || suffix == ".bmp" || suffix == ".BMP")) {
        $.alert("请选择正确格式的图片文件", 2);
        return false;
    }
}
// 关闭图片上传Modal并清空数据
$.closeImageUpload = function () {
    $("#modal-upload-image").modal("hide");
    $("#ossfile").empty();
    clearOssData();
};
// 获取上传图片的信息
$.getImageInfo = function () {
    var fileInfo = getOssFileInfo();
    if ((fileInfo.length) == 0) {
        $.alert("请上传一张图像", 2);
    } else if ((fileInfo.length) > 1) {
        $.alert("只能上传一张图像", 2);
    } else {
        imageUploadUrl = fileInfo[0].fileUrl;
        $("#userIcon").val(imageUploadUrl);
        $("#myPhoto").attr('src', src = imageUploadUrl);
        $.closeImageUpload();
    }
};